package com.ruoyi.web.controller.frock;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.Date;
import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.base.BaseShop;
import com.ruoyi.system.domain.frock.FrockUse;
import com.ruoyi.system.mapper.BaseShopMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.IFrockUseService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 工装使用详情Controller
 *
 * @author ruoyi
 * @date 2023-04-21
 */
@Api(tags = "工装使用详情Api")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/frock/use")
public class FrockUseController extends BaseController {

    private final IFrockUseService iFrockUseService;

    /**
     * 查询工装使用详情列表
     */
    @ApiOperation(value = "查询工装使用详情列表")
    //@PreAuthorize("@ss.hasPermi('frock:use:list')")
    @GetMapping("/list")
    public TableDataInfo list(FrockUse frockUse)
    {
        startPage();
        LambdaQueryWrapper<FrockUse> lqw = new LambdaQueryWrapper<FrockUse>();
        if (StringUtils.isNotBlank(frockUse.getNumbers())){
            lqw.eq(FrockUse::getNumbers ,frockUse.getNumbers());
        }
        if (StringUtils.isNotBlank(frockUse.getFrockCode())){
            lqw.eq(FrockUse::getFrockCode ,frockUse.getFrockCode());
        }
        if (StringUtils.isNotBlank(frockUse.getFrockName())){
            lqw.like(FrockUse::getFrockName ,frockUse.getFrockName());
        }
        if (frockUse.getShopId()!=null){
            lqw.eq(FrockUse::getShopId ,frockUse.getShopId());
        }
        if (frockUse.getBindingDate() != null){
            lqw.eq(FrockUse::getBindingDate ,frockUse.getBindingDate());
        }
        if (frockUse.getUnbindingDate() != null){
            lqw.eq(FrockUse::getUnbindingDate ,frockUse.getUnbindingDate());
        }
        if (frockUse.getStatus() != null){
            lqw.eq(FrockUse::getStatus ,frockUse.getStatus());
        }
        if (frockUse.getUsageDuration() != null){
            lqw.eq(FrockUse::getUsageDuration ,frockUse.getUsageDuration());
        }
        if (frockUse.getUsageCount() != null){
            lqw.eq(FrockUse::getUsageCount ,frockUse.getUsageCount());
        }
        if (frockUse.getUsageDurationLj() != null){
            lqw.eq(FrockUse::getUsageDurationLj ,frockUse.getUsageDurationLj());
        }
        if (frockUse.getUsageCountLj() != null){
            lqw.eq(FrockUse::getUsageCountLj ,frockUse.getUsageCountLj());
        }
        List<FrockUse> list = iFrockUseService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出工装使用详情列表
     */
    //@PreAuthorize("@ss.hasPermi('frock:use:export')" )
    @Log(title = "工装使用详情" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(FrockUse frockUse) {
        LambdaQueryWrapper<FrockUse> lqw = new LambdaQueryWrapper<FrockUse>(frockUse);
        List<FrockUse> list = iFrockUseService.list(lqw);
        ExcelUtil<FrockUse> util = new ExcelUtil<FrockUse>(FrockUse. class);
        return util.exportExcel(list, "use" );
    }

    /**
     * 获取工装使用详情详细信息
     */
    @ApiOperation(value = "获取工装使用详情详细信息")
    //@PreAuthorize("@ss.hasPermi('frock:use:query')" )
    @GetMapping(value = "/{useId}" )
    public AjaxResult getInfo(@PathVariable("useId" ) Long useId) {
        return AjaxResult.success(iFrockUseService.getById(useId));
    }

    @Autowired
    private BaseShopMapper baseShopMapper;
    /**
     * 新增工装使用详情
     */
    @ApiOperation(value = "新增工装使用详情")
    //@PreAuthorize("@ss.hasPermi('frock:use:add')" )
    @Log(title = "工装使用详情" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FrockUse frockUse) {
        frockUse.setCreateBy(SecurityUtils.getUsername());
        frockUse.setCreateTime(new Date());
        //根据工位id查询工位名称
        if(frockUse.getShopId()!=null){
            BaseShop shop = baseShopMapper.selectById(frockUse.getShopId());
            frockUse.setShopName(shop.getShopName());
        }
        return toAjax(iFrockUseService.save(frockUse) ? 1 : 0);
    }

    /**
     * 修改工装使用详情
     */
    @ApiOperation(value = "修改工装使用详情")
    //@PreAuthorize("@ss.hasPermi('frock:use:edit')" )
    @Log(title = "工装使用详情" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FrockUse frockUse) {
        return toAjax(iFrockUseService.updateById(frockUse) ? 1 : 0);
    }

    /**
     * 删除工装使用详情
     */
    @ApiOperation(value = "删除工装使用详情")
    //@PreAuthorize("@ss.hasPermi('frock:use:remove')" )
    @Log(title = "工装使用详情" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{useIds}" )
    public AjaxResult remove(@PathVariable Long[] useIds) {
        return toAjax(iFrockUseService.removeByIds(Arrays.asList(useIds)) ? 1 : 0);
    }
}
